angular.module('starter.auth.service', [])


  .factory('$$AuthService', function($rootScope, $http, $$HttpService) {
    console.info("factory----------$$AuthService");
    return {
      // 登录
      login: function(telephone, password) {
        var req = {
          telephone: telephone,
          password: password,
        };
        return $$HttpService.timepost("api/user/login", req);
      },

      // 注册
      register: function(registerData) {
        var req = {
          telephone: registerData.telephone,
          password: registerData.password,
          validateCode: registerData.validateCode,
          // nickname: nickname,
        };
        return $$HttpService.timepost("api/user/regist", req);
      },

      // 第三方登录后绑定用户信息
      // 第三方登录后未注册手机号需完善
      // 微信
      registerthirdpart: function(registerthirdpartData) {
        var req = {
          telephone: registerthirdpartData.telephone,
          password: registerthirdpartData.password,
          validateCode: registerthirdpartData.validateCode,
          openid: registerthirdpartData.openid,
          // openid: "oESGLwT0mEK_9V4lcqKfPTfvepws"
        };
        return $$HttpService.timepost("api/user/bindUser", req);
      },

      // 发送注册 获取验证码
      sendRegisterCode: function(telephone) {
        var req = {
          telephone: telephone,
        };
        return $$HttpService.timeget("api/options/validateCode", req);
      },

      // 修改用户密码
      modifyPassword: function(passwordData) {
        var req = {
          telephone: passwordData.telephone,
          password: passwordData.password,
          validateCode: passwordData.validateCode,
        };
        return $$HttpService.timepost("api/user/modifyPassword", req);
      },

      // 获取用户个人信息
      getuserInformation: function() {
        var req = {};
        return $$HttpService.timeget("api/user/userInformation", req);
      },

      // 保存用户地理位置信息
      usergeoLocation: function(position) {
        var req = {
          lng: "" + position.lng,
          lat: "" + position.lat
        };
        return $$HttpService.timeget("api/user/geoLocation", req);
      },

      // 三方登录 微信
      weixinloginsendCode: function(code) {
        var req = {
          code: code
        };
        return $$HttpService.timeget("api/otherLogin/weChat", req);
      },

      // 三方登录 微信 需要三方登录后绑定用户信息
      weixinloginbindUser: function(code) {
        var req = {
          code: code
        };
        return $$HttpService.timeget("api/user/bindUser", req);
      },





    };
  })

  .factory('$$UserDataService', function($rootScope, $http, $$LocalStorage) {
    console.info("factory----------$$UserDataService");
    return {
      getCacheUserData: function() {
        var userData = $$LocalStorage.getObj("userData");
        if (userData == null) {
          userData = {
            id: null,
            nickName: "未登录",
            userName: "",
            headPic: "https://www.bannitongyou.com/pic/tx.png",
            telephone: "",
            token: "",
            signature: "",
            type: 0,
            isRealNameAuthentication: 0,
            friendVO: {
              attentionsCount: 0,
              fansCount: 0,
            }
          };
        }
        return userData;
      },
      updateUserData: function(userData) {
        $rootScope.userData = userData;
        $$LocalStorage.setObj("userData", userData);
        return $rootScope.userData;
      },
      clearUserData: function(userData) {
        $$LocalStorage.remove("userData");
        $rootScope.userData = this.getCacheUserData();
        $rootScope.isLogined();
        console.log($rootScope.userData);
        return $rootScope.userData;
      }
    };
  })



  .factory('$$WeixinConfigService', function($rootScope, $http, $$SignService) {
    console.info("factory----------$$WeixinConfigService");
    return {
      // 获取 微信JS SDK 配置参数
      getWeixinJSConfig: function() {

        var data = {
          apiUrl: "api/v1/pay/wxJssdk.api",
          url: "https://m.badouxueche.com/"
        };
        return $http.post($rootScope.config.apiHost + data.apiUrl, $$SignService.sign(data));
      }
    };
  })

;
